import { action } from 'mobx';

import ListBaseStore from './ListBaseStore';
import * as ApplyApis from '../apis/apply';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

/**
 * 我的帖子Store
 */
export default class MineStore extends ListBaseStore {
	constructor() {
		super();

		this.title = getLabel(33842, '协作申请');
		this.selectedKey = 'apply'; // 默认是申请加入
	}

	@action doInit = () => {
    this.selectedKey = 'apply';
    this.doBaseInit();
    this.initConditionDatas(); // 初始化高级搜索区域
    this.doTableLoading(); // 初始化列表
    this.getCountsInfos(); // 初始化顶部tab统计数据
	}

	// 加载列表数据
	@action doTableLoading = (params) => {
		this.showLoading(true);

		let resultParams = this.getFormParams();

		let selectedKey = this.selectedKey ? this.selectedKey : 'apply';
		let commonParams = {
			topKey: selectedKey, // 查看页面类型，选中的tab  key
		};

		resultParams = { ...resultParams, ...commonParams, ...params };

		ApplyApis.getCoworkApplyPageList(resultParams).then(
			(result) => {
				const sessionkey = result.sessionkey;
				this.sessionkey = sessionkey;
				this.comsWeaTableStore.getDatas(sessionkey, 1);

				this.showLoading(false);
			},
		);

		if (selectedKey == 'approval') {
			this.getCountsInfos();
		}
	}

	// 初始化收藏的帖子页面查询条件
	@action initConditionDatas = (params) => {
		ApplyApis.getCoworkApplyPageCondition(params).then(
			(result) => {
				const { apply, approval, applyLog, applyquited, topTab } = result;
				let conditioninfo = [apply, approval, applyLog, applyquited];
				this.topTab = topTab;
				this.conditioninfo = conditioninfo;

				this.initFormFields(apply); // 一定要先初始化initFormFields表单对象
			},
		);
	}

	// 查询页面总数
	@action getCountsInfos = (params) => {
		ApplyApis.getCoworkApplyPageTopCounts(params).then(
			(result) => {
				this.topTabCounts = result.topCounts;
			},
		);
	}

	// 批量申请、申请加入
	@action coworkApplayInfo = (params, callback) => {
		ApplyApis.coworkApplayInfo(params).then(
			() => {
				if (callback && typeof callback === 'function') {
					callback();
				}
			},
		);
	}

	// 批量批准、批量拒绝 (status: 0为拒绝，1为批准)
	@action coworkApplayApproveInfo = (params, callback, callback2) => {
		ApplyApis.coworkApplayApproveInfo(params).then(
			(result) => {
				let newTopTabCounts = this.topTabCounts;
				newTopTabCounts.approval = result.approveCount;
				this.topTabCounts = newTopTabCounts;

				if (callback && typeof callback === 'function') {
					callback();
        }
        if (callback2 && typeof callback2 === 'function') {
					callback2();
				}
			},
		);
	}

	// 获取申请审批总数量
	@action getCoworkApplyPageTopCounts = (params, callback) => {
		ApplyApis.getCoworkApplyPageTopCounts(params).then(
			() => {
				if (callback && typeof callback === 'function') {
					callback();
				}
			},
		);
	}
}
